<?php
/**
 * 匠言知识付费
 * Author: 山西匠言网络科技有限公司
 * This is not a free software, it under the license terms, you can visit https://www.zsffzxkc.cn/ get more details.
 */
namespace app\index\controller;
use app\index\controller\Base;
use think\Controller;
use think\Db;
class Index extends  Base
{
    public function index(){
        $type=input('param.type');
        $map2=array(
            'status'=>1,
            'acid'=>1
        );
        $data = [];
        if($type == 'mobile' || !isset($type)){
            $ads = Db::name('ads')->where($map2)->where('shoptype','zsff')->select();
            if(!empty($ads)) {
                foreach ($ads as $k => $v) {
                    $ads[$k]['thumb'] = to_media_url($ads[$k]['thumb']);
                }
            }
            $classify=Db::name('classify')->where($map2)->where('pid',0)->order('sort asc')->select();
            $data['ads'] = $ads;
            $data['classify'] = $classify;
        }
        elseif($type=='pc'){
            $classify=Db::name('classify')->where($map2)->where('pid',0)->limit(6)->select();
            //构建PC端分类
            $pcclassify=[];
            foreach ($classify as $k=>$v){
                $pcclassify[$k]['name']=$v['title'];
                $pcclassify[$k]['id']=$v['id'];
                $subclass=db('classify')->where($map2)->where('pid',$v['id'])->select();
                if(!empty($subclass)){
                    $item=[];
                    foreach ($subclass as $key=>$val){
                        array_push($item,['name'=>$val['title'],'id'=>$val['id']]);
                    }
                    $pcclassify[$k]['subclass']=$item;
                }else{
                    $pcclassify[$k]['subclass']=[];
                }
            }
            $data['pcclass']=$pcclassify;
            //获取PC端设置
            $pc_set=Db::name('set_pc')
                ->where(['acid'=>1])
                ->find();
            $data['pc_set']=$pc_set;
            $pcads = Db::name('ads')->where($map2)->where('shoptype','pc')->select();
            if(!empty($pcads)) {
                foreach ($pcads as $k => $v) {
                    $pcads[$k]['thumb'] = to_media_url($pcads[$k]['thumb']);
                }
            }
            $data['pc_ads']=$pcads;
        }
        $base_set=Db::name('set')
            ->where(['acid'=>1])
            ->find();
        //推荐课程
        $courses=db('coursemenu')->where($map2)->where(['is_tj'=>1,'is_verify'=>1])->limit(10)->select();
        foreach ($courses as $k=>$v){
            if($v['media']=='audio'){
                $courses[$k]['mediatext']='音频';
            }elseif($v['media']=='video'){
                $courses[$k]['mediatext']='视频';
            }elseif($v['media']=='tuwen'){
                $courses[$k]['mediatext']='图文';
            }elseif($v['media']=='all'){
                $courses[$k]['mediatext']='混合';
            }
            $ksnum=db('course')->where($map2)->where(['menuid'=>$v['id']])->count();
            $courses[$k]['ksnum']=$ksnum;
            if($v['lid']!=0){
                $levelinfo = db('viplevel')->where(['acid'=>1,'id'=>$v['lid']])->find();
                $courses[$k]['levelname']=$levelinfo['name'];
            }else{
                $courses[$k]['levelname']='不限';
            }
            $courses[$k]['thumb'] = to_media_url($courses[$k]['thumb']);
        }
        //拼团
        $pt=Db::name('pintuan')
            ->where($map2)
            ->where('starttime','lt',time())
            ->where('endtime','gt',time())
            ->order('addtime desc')
            ->select();
        foreach ($pt as $k=>$v){
            if($v['goodstype']=='course'){
                $db=db('coursemenu');
            }elseif($v['goodstype']=='mall'){
                $db=db('goods');
            }elseif($v['goodstype']=='pxb'){
                $db=db('pxcourse');
            }
            $pt[$k]['goodsinfo']=$db->where($map2)->where('id',$v['goodsid'])->find();
            if(!empty($pt[$k]['goodsinfo']) && !empty($pt[$k]['goodsinfo']['thumb'])) {
                $thumbs = explode(',', $pt[$k]['goodsinfo']['thumb']);
                foreach ($thumbs as &$v1) {
                    $v1 = to_media_url($v1);
                }
                $pt[$k]['goodsinfo']['thumb'] = implode(',', $thumbs);
            }
        }
        // 培训机构
        $pxbs = Db::name('peixunban')
            ->where($map2)
            ->limit(4)
            ->select();
        foreach ($pxbs as $k=>$v){
            $thumb=explode(',',$v['thumb']);
            if(!empty($thumb)) {
                foreach ($thumb as &$v1) {
                    $v1 = to_media_url($v1);
                }
                $pxbs[$k]['thumb'] = implode(',', $thumb);
            }
            $pxbs[$k]['main_thumb']=$thumb[0];
            $classifyname='';
            if(strpos($v['fenleiid'],',')){
                $fls=explode(",",$v['fenleiid']);
                foreach ($fls as $key=>$val){
                    $title=Db::name('pxb_classify')
                        ->field('title')
                        ->where('id',$val)
                        ->find();
                    $classifyname.=($title['title'].' / ');
                }
                $classifyname=rtrim($classifyname,' /');
            }else{
                $title=Db::name('pxb_classify')
                    ->field('title')
                    ->where('id',$v['fenleiid'])
                    ->find();
                $classifyname=$title['title'];
            }
            $pxbs[$k]['classify']=$classifyname;
        }
        $data['base_set']=$base_set;
        $data['courses']=$courses;
        $data['pt']=$pt;
        $data['pxjg']=$pxbs;
        $this->result(0,'获取首页数据成功',$data,0,'json');
    }
    public function indextjlist(){
        //推荐课程
        $courses=db('coursemenu')->where(['acid'=>1,'status'=>1])->select();
        foreach ($courses as $k=>$v){
            $ksnum=db('course')->where(['acid'=>1,'status'=>1,'menuid'=>$v['id']])->count();
            $courses[$k]['ksnum']=$ksnum;
            $courses[$k]['thumb']=to_media_url($v['thumb']);
            if($v['media']=='audio'){
                $courses[$k]['mediatext']='音频';
            }elseif($v['media']=='video'){
                $courses[$k]['mediatext']='视频';
            }elseif($v['media']=='tuwen'){
                $courses[$k]['mediatext']='图文';
            }elseif($v['media']=='all'){
                $courses[$k]['mediatext']='混合';
            }
            if($v['lid']!=0){
                $levelinfo = db('viplevel')->where(['acid'=>1,'id'=>$v['lid']])->find();
                $courses[$k]['levelname']=$levelinfo['name'];
            }else{
                $courses[$k]['levelname']='不限';
            }
        }
        if($courses){
            $this->result(0,'获取推荐课程数据成功',$courses,0,'json');
        }else{
            $this->result(1,'暂无推荐课程','',0,'json');
        }
    }
    //更多视频
    public function indexsplist(){
        $set=$this->get_set('BASE');

        $Nowpage = input('post.page') ? input('post.page'):0;
        $limits =isset($set['loadmorenum'])?$set['loadmorenum']:6;

        $videos=db('resources')->where(['acid'=>1,'state'=>1])->select();
        foreach ($videos as $k=>$v){
            $videos[$k]['create_time']=date('Y-m-d H:i:s',$v['create_time']);
        }
        if (!empty($videos)){
            $page_videos=array_slice($videos,$Nowpage*$limits,$limits);
        }
        if(!empty($page_videos)){
            $this->result(0,'获取更多视频数据成功',$page_videos,0,'json');
        }else{
            $this->result(1,'暂无更多视频','',0,'json');
        }
    }
    public function dhhyp(){
        $post=input('post.');
        $Nowpage = input('post.page') ? input('post.page'):0;
        $limits =2;
        $limit=$Nowpage*$limits.','.$limits;
        $is_tj=$post['type']=='tj'?1:0;
        $courses=db('coursemenu')->where(['acid'=>1,'status'=>1,'is_tj'=>$is_tj])
            ->where('fenleiid','like','%'.$post['flid'].'%')
            ->limit($limit)->select();
        foreach ($courses as $k=>$v){
            $ksnum=db('course')->where(['acid'=>1,'status'=>1,'menuid'=>$v['id']])->count();
            $courses[$k]['ksnum']=$ksnum;
            if($v['lid']!=0){
                $levelinfo = db('viplevel')->where(['acid'=>1,'id'=>$v['lid']])->find();
                $courses[$k]['levelname']=$levelinfo['name'];
            }else{
                $courses[$k]['levelname']='不限';
            }
            $courses[$k]['thumb']= to_media_url($courses[$k]['thumb']);
        }
        if($courses){
            $this->result(0,'获取导航换一批课程列表数据成功',$courses,0,'json');
        }else{
            $this->result(1,'暂无课程','',0,'json');
        }
    }
}
